﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sami_SMS_CRM_Business;
using System.Web.Services;

namespace Sami_SMS_CRM_UI
{
    public partial class crm_back_list : BasePage
    {
        BackListRepository repository = new BackListRepository();
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            pagingtop.PagingChange += new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange += new EventHandler(paging_PagingChange);
        }

        void paging_PagingChange(object sender, EventArgs e)
        {
            pagingtop.PagingChange -= new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange -= new EventHandler(paging_PagingChange);
            pagingtop.CurrentPage = (sender as UserControl.ucPaging).CurrentPage;
            pagingbottom.CurrentPage = (sender as UserControl.ucPaging).CurrentPage;
            BindDataTable();
            pagingtop.Visible = (sender as UserControl.ucPaging).TotalPage > 1;
            pagingtop.PagingChange += new EventHandler(paging_PagingChange);
            pagingbottom.Visible = (sender as UserControl.ucPaging).TotalPage > 1;
            pagingbottom.PagingChange += new EventHandler(paging_PagingChange);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
                pagingtop.CurrentPage = 1;
                pagingtop.PageSize = PageSize;
                pagingtop.PageDisplay = PageDisPlay;
                pagingbottom.CurrentPage = 1;
                pagingbottom.PageSize = PageSize;
                pagingbottom.PageDisplay = PageDisPlay;
                BindDataTable();
            }
        }

        private void BindDataTable()
        {
            int currentPage = pagingtop.CurrentPage;
            int pageSize = pagingtop.PageSize;
            int totalRecord = 0;

            string subs = txtSubscriber.Text.Trim();
            bool? enable = ddlEnable.SelectedValue == "true" ? true : (ddlEnable.SelectedValue == "false" ? false : (bool?)null);

            rptTable.DataSource = repository.GetAll(currentPage, pageSize, ref totalRecord, subs, enable);
            rptTable.DataBind();

            pagingtop.TotalRecord = totalRecord;
            pagingtop.Visible = pagingtop.TotalPage > 1;
            pagingbottom.TotalRecord = totalRecord;
            pagingbottom.Visible = pagingtop.TotalPage > 1;
            ltrTotalRow.Text = totalRecord.ToString();
        }

        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            pagingtop.PageSize = pagingbottom.PageSize = int.Parse((sender as DropDownList).SelectedValue);
            BindDataTable();
        }

        protected void rptTable_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                if (IsAdmin())
                {
                    string id = e.CommandArgument.ToString();
                    switch (e.CommandName)
                    {
                        case "Edited":
                            var entity = repository.GetById(id);
                            SetData(entity);
                            ScriptManager.RegisterStartupScript(this, this.GetType(), "", "dialog();", true);
                            break;
                        case "Deleted":
                            repository.DeleteById(id);
                            BindDataTable();
                            break;
                    }
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert(\"You don't have permission!\");", true);
                }
            }
            catch (Exception ex)
            {
                new LogRepository().InsertLog(ex);
            }
        }

        protected void btnSearching_Click(object sender, EventArgs e)
        {
            BindDataTable();
        }

        protected void btnAddNew_Click(object sender, ImageClickEventArgs e)
        {
            SetData(null);
            ScriptManager.RegisterStartupScript(this, this.GetType(), "", "dialog();", true);
        }

        private void SetData(Sami_SMS_CRM_Business.crm_black_list entity)
        {
            if (entity != null)
            {
                hdfAction.Value = "Edit";
                hdfId.Value = entity.subscriber;
                txtSubs.Text = entity.subscriber;
                txtSubs.Enabled = false;
                ddlEnabled.SelectedValue = entity.enabled.ToString();
                txtDescription.Text = entity.description;
                txtOperator_Id.Text = entity.operator_id.ToString();
            }
            else
            {
                hdfAction.Value = "AddNew";
                hdfId.Value = string.Empty;
                txtSubs.Text = string.Empty;
                txtSubs.Enabled = true;
                txtDescription.Text = string.Empty;
                txtOperator_Id.Text = "0";
                ddlEnabled.SelectedIndex = 0;
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                if (CheckFullInfo())
                {
                    Sami_SMS_CRM_Business.crm_black_list entity = repository.GetById(hdfId.Value) ?? new Sami_SMS_CRM_Business.crm_black_list();
                    entity.enabled = bool.Parse(ddlEnabled.SelectedValue);
                    entity.description = txtDescription.Text;
                    entity.operator_id = int.Parse(txtOperator_Id.Text);
                    switch (hdfAction.Value)
                    {
                        case "Edit":
                            repository.Update(entity);
                            BindDataTable();
                            break;
                        case "AddNew":
                            entity.subscriber = txtSubs.Text.Trim();
                            repository.AddNew(entity);
                            BindDataTable();
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                new LogRepository().InsertLog(ex);
            }
        }

        private bool CheckFullInfo()
        {
            string errMsg = string.Empty;
            if(string.IsNullOrEmpty(txtSubs.Text.Trim()))
                errMsg="Subscriber not allow null";
            if (string.IsNullOrEmpty(txtOperator_Id.Text.Trim()))
                errMsg = "Operator_id not allow null";
            if (errMsg == string.Empty)
            {
                return true;
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert(\"" + errMsg + "\"); dialog();", true);
                return false;
            }
        }
    }
}